package ldh.im.fxbase.dao;

import ldh.im.fxbase.data.pojo.ImCollect;
import ldh.im.fxbase.data.pojo.ImConfig;
import ldh.im.fxbase.data.pojo.ext.ImConfigExt;
import ldh.im.fxbase.util.DbUtil;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

public class ImConfigDao extends BaseDao {

    private static final String TABLE_NAME = "im_config";

    public ImConfigExt getByUserIdAndName(String userId, String name) throws SQLException {
        return DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("select * from im_config where name = ? and currentUserId = ?");
            ImConfigExt imConfig = queryRunner.query(connection, sql, new BeanHandler<>(ImConfigExt.class), name, userId);
            return imConfig;
        });
    }

    public List<ImConfig> getAll() throws SQLException {
        return DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("select * from im_config");
            List<ImConfigExt> configList = queryRunner.query(connection, sql, new BeanListHandler<>(ImConfigExt.class));
            List<ImConfig> configs = configList.stream().map(c-> (ImConfig)c).collect(Collectors.toList());
            return configs;
        });
    }

    public void batchInserts(List<ImConfig> imConfigs) throws SQLException {
        if (imConfigs.size() < 1) return;
        Object[][] params = imConfigs.stream().map(imConfig-> new Object[]{imConfig.getName(), imConfig.getCurrentUserId(), imConfig.getContent(),
                valueEnum(imConfig.getStatus()), imConfig.getCreateTime(), imConfig.getUpdateTime()}).toArray(Object[][]::new);
        DbUtil.transaction((queryRunner, connection) -> {
            ColumnListHandler<Long> resultSetHandler = new ColumnListHandler();
            String sql = String.format("insert into im_config(name, currentUserId, content, status,  createTime, updateTime) " +
                    "values(?, ?, ?, ?, ?, ?)");
            List<Long> ids = queryRunner.insertBatch(connection, sql, resultSetHandler, params);
            int idx = 0;
            for(Long id : ids) {
                imConfigs.get(idx++).setId(id);
            }
            return ids.size();
        });
    }

    public void batchUpdates(List<ImConfig> imConfigs) throws SQLException {
        if (imConfigs.size() < 1) return;
        Object[][] params = imConfigs.stream().map(imConfig-> new Object[]{imConfig.getContent(), valueEnum(imConfig.getStatus()),  imConfig.getUpdateTime(), imConfig.getId()}).toArray(Object[][]::new);

        DbUtil.transaction((queryRunner, connection) -> {
            ResultSetHandler<Long> resultSetHandler = new ScalarHandler();
            String sql = String.format("update im_collect set content = ?, status =?, updateTime = ? where id = ? ");
            int[] ns = queryRunner.batch(connection, sql, params);
            if (ns.length != imConfigs.size()) {
                throw new RuntimeException("更新数据失败");
            }
            return ns.length;
        });
    }


}
